<template>
  <div>
    <router-link to="/index/role/add">
      <el-button>添加</el-button>
    </router-link>
    <el-table
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
    >
      <el-table-column prop="id" label="ID" width="180"> </el-table-column>
      <el-table-column prop="rolename" label="角色名称" width="180">
      </el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 1" type="success">正常</el-tag>
          <el-tag v-else>禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            type="primary"
            circle
            size="mini"
            @click="edit(scope.row.id)"
            icon="el-icon-edit"
          ></el-button>
          <el-button
            type="warning"
            size="mini"
            icon="el-icon-view"
            @click="view(scope.row)"
            circle
          ></el-button>
          <el-button
            type="danger"
            circle
            size="mini"
            @click="del(scope.row.id)"
            icon="el-icon-delete"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 查看权限 -->
    <!-- visible.sync同步切换对话框的显示隐藏 -->
    <el-dialog title="提示" :visible.sync="dialogVisible" width="50%">
      <!-- data树形控件的数据 -->
      <!-- show-checkbox显示选框 -->
      <!-- default-expand-all默认展开所有选项 -->
      <!-- node-key:id 通过id选择节点 -->
      <!-- highlight-current高亮选中项 -->
      <!-- props：其他选项，包含文字显示和子节点 -->
      <el-tree
        :data="menuList"
        show-checkbox
        default-expand-all
        node-key="id"
        ref="tree"
        :check-strictly="isStrictly"
        highlight-current
        :props="defaultProps"
      >
      </el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      tableData: [],
      // 弹窗可见性
      dialogVisible: false,
      // 角色权限对应的树形控件数据
      menuList:[],
      defaultProps: {
        children: 'children',
        label: 'title'
      },
      // 非严格模式
      isStrictly: false
    };
  },
  created() {
    this.getData();
    // 获取树形控件的内容
    this.getMenuData();
  },
  methods: {
    // 获取表格的数据
    getData() {
      this.$http.get("rolelist").then((res) => {
        this.tableData = res.list;
      });
    },
    // 获取树形控件的数据，注意istree=1
    getMenuData() {
      this.$http('/menulist',{params: {istree: 1}})
        .then(res => {
          this.menuList = res.list;
        })
    },
    // 编辑
    edit(id) {
      this.$router.push("/index/role/" + id);
    },
    // 删除,需要先弹窗二次确认
    del(id) {
      this.$confirm("确认关闭？")
        .then((_) => {
          // 确认删除
          this.doDel(id);
        })
        .catch((_) => {
          // 关闭弹窗
        });
    },
    doDel(id) {
      this.$http.post("/roledelete", { id }).then((res) => {
        this.tableData = res.list;
      });
    },
    view(info) {
      let menus = info.menus.split(",");
      this.dialogVisible = true;
      // 设置为树形控件严格模式，父子互不关联！  
      this.isStrictly = true;
      this.$nextTick(()=>{
        // 回显数据！设置对应显示的！ 严格模式！
        this.$refs.tree.setCheckedKeys(menus);  
        // 非严格模式！
        this.isStrictly = false;
      })
    },
  },
};
</script>
<style lang="">
</style>